package micro

import (
	"fmt"
	"gf-app/app/model/in_warehouse_infos"
	"gf-app/app/model/p_201__delivers"
	"gf-app/frame/g"
	gin "github.com/gin-gonic/gin"
	"github.com/gogf/gf/os/gtime"
	"net/http"
	"strconv"
)

func init() {
	s := g.GinEngine()
	fmt.Printf("------ micro router init, s=%v\n", s)
	v1 := s.Group("/v1")
	{
		v1.GET("/news", func(c *gin.Context) {
			c.JSON(200, gin.H{
				"code": "200",
				"msg":  "哈哈，新闻，你个开心果",
			})
		})
	}

	p201 := s.Group("/p201")
	{
		p201.GET("/api/list", func(c *gin.Context) {
			c.String(200, "%v", "接口列表")
		})

		// 入库信息，列表
		p201.GET("/api/inwarehouse/list", func(c *gin.Context) {
			list, err := in_warehouse_infos.FindAll()
			if err != nil {
				c.JSON(404, gin.H{"error": err.Error()})
				return
			}

			// --------------- 返回结果 -------------
			c.JSON(200, gin.H{
				"status":  "200",
				"message": "检索入库信息成功，count=" + strconv.Itoa(len(list)),
				"data":    list,
			})

		})

		// 入库信息提交
		p201.POST("/api/inwarehouse", func(c *gin.Context) {
			var json in_warehouse_infos.Entity
			if err := c.ShouldBindJSON(&json); err != nil {
				c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
				return
			}

			// --------------- 更新字段 ------------------
			json.CreatedAt = gtime.Now()

			// --------------- 写入数据库 ----------------
			rs, err := json.Insert()

			// --------------- 返回结果 -------------
			c.JSON(200, gin.H{
				"status":  "200",
				"message": "入库信息提交成功",
				"data":    json,
				"err":     err,
				"rs":      rs,
			})
		})

		// 出货信息，列表
		p201.GET("/api/outwarehouse/list", func(c *gin.Context) {
			list, err := p_201__delivers.FindAll()
			if err != nil {
				c.JSON(404, gin.H{"error": err.Error()})
				return
			}

			// --------------- 返回结果 -------------
			c.JSON(200, gin.H{
				"status":  "200",
				"message": "检索出货信息成功，count=" + strconv.Itoa(len(list)),
				"data":    list,
			})

		})
		// 出货信息提交
		p201.POST("/api/outwarehouse/insert", func(c *gin.Context) {
			var json p_201__delivers.Entity
			if err := c.ShouldBindJSON(&json); err != nil {
				c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
				return
			}

			// --------------- 更新字段 ------------------
			json.CreatedAt = gtime.Now()

			// --------------- 写入数据库 ----------------
			rs, err := json.Insert()

			// --------------- 返回结果 -------------
			c.JSON(200, gin.H{
				"status":  "200",
				"message": "出货信息提交成功",
				"data":    json,
				"err":     err,
				"rs":      rs,
			})
		})
	}

}
